package com.dwin.dingnuo.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import java.util.Stack;

import androidx.fragment.app.Fragment;

/**
 * 应用程序Activity管理类：用于Activity管理和应用程序
 */
public class ActivityUtils {
    private String TAG = ActivityUtils.class.getSimpleName();
    private static Stack<Activity> activityStack;
    private static ActivityUtils instance;

    private ActivityUtils() {
    }

    /**
     * 单一实例
     */
    public static ActivityUtils getInstance() {
        if (instance == null) {
            instance = new ActivityUtils();
        }
        return instance;
    }

    /**
     * 添加Activity
     */
    public void addActivity(Activity activity) {
//        LogUtils.e(TAG,"add:"+activity.getClass().getName());
        if (activityStack == null) {
            activityStack = new Stack<Activity>();
        }
        activityStack.add(activity);
    }

    /**
     * 结束指定的Activity
     */
    public void finishActivity(Activity activity) {
        if (activity != null) {
            activityStack.remove(activity);
            activity.finish();
            activity = null;
        }
    }

    /**
     * 结束所有Activity
     */
    public void finishAllActivity() {
        if(activityStack==null){
            return;
        }
        for (Activity a : activityStack) {
            if (a != null) {
                a.finish();
            }
        }
        activityStack.clear();
    }

    /**
     * @param className
     * @Title: finishActivityExpet
     * @Description: 结束除了指定Activity之外的所有Activity
     * @return: void
     * @author: eye_fa
     */
    public void finishActivityExpet(String className) {
        if (activityStack != null) {
            for (Activity activity : activityStack) {
                if (activity != null && !activity.getClass().getName().equals(className)) {
                    activity.finish();
                }
            }
        }
    }


    /**
     * @param className
     * @Title: finishActivityForName
     * @Description: 根据名字结束除了指定Activity
     * @return: void
     * @author: eye_fa
     */
    public void finishActivityForName(String className) {
        if (activityStack != null) {
            for (Activity activity : activityStack) {
//                LogUtils.e(TAG,activity.getClass().getName());
                if (activity != null && activity.getClass().getName().equals(className)) {
                    activity.finish();
                }
            }
        }
    }

    /**
     * 切换 Activity
     */
    public static void switchActivity(Context context, Class<?> clas) {
        switchActivity(context, clas, null);
    }

    /**
     * 切换 Activity
     */
    public static void switchActivityFinish(Context context, Class<?> clas) {
        switchActivity(context, clas, null);
        Activity activity = (Activity) context;
        activity.finish();
    }

    /**
     * 切换 Activity
     */
    public static void switchActivity(Context context, Class<?> clas, Bundle bundle) {
        Intent intent = new Intent(context, clas);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        context.startActivity(intent);
    }

    /**
     * 切换 Activity
     */
    public static void switchActivityForResult(Activity context, Class<?> clas, Bundle bundle, int requestcode) {
        Intent intent = new Intent(context, clas);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        context.startActivityForResult(intent,requestcode);
    }

    /**
     * 切换 Activity
     */
    public static void switchActivityForResult(Fragment fragment, Context context, Class<?> clas, Bundle bundle, int requestcode) {
        Intent intent = new Intent(context, clas);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        fragment.startActivityForResult(intent,requestcode);
    }

    /**
     * 切换 Activity
     */
    public static void switchActivityFinish(Context context, Class<?> clas, Bundle bundle) {
        Intent intent = new Intent(context, clas);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        context.startActivity(intent);
        Activity activity = (Activity) context;
        activity.finish();
    }

    /**
     * 切换 Activity
     */
    public static void switchActivityFinish(Context context, Intent intent, Bundle bundle) {
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        context.startActivity(intent);
        Activity activity = (Activity) context;
        activity.finish();
    }




}